<?php

namespace app\xwam\model;

/**
 * WebsiteConf 模型类
 *
 * @version VER:1.0; DATE:2016-8-22; AUTHOR:SoChishun; EMAIL:14507247@qq.com; DESC:Added.
 */
class WebsiteConfModel extends AppbaseModel {

    protected $table = 't_system_website';

    /**
     * 获取id_text列表
     * @param boolean $key_is_id 键名是否是ID(默认false)
     * @return type
     * @version 1.0 2016-5-27 SoChishun Added.
     */
    function getIdTextList($key_is_id = false) {
        $list = array();
        if ($key_is_id) {
            $list = $this->where(array('status' => 1))->cache('site_idtext_kvlist', 10)->order('sort, id')->getField('id, site_name as text');
        } else {
            $list = $this->where(array('status' => 1))->cache('site_idtext_list', 10)->order('sort, id')->field('id, site_name as text')->select();
        }
        return $list;
    }

    /**
     * 获取网站
     * @param type $siteId
     * @param type $userName
     * @param type $create
     * @return type
     * @version 1.0 2016-4-21 SoChishun Added.
     */
    public function findSite($siteId, $userName, $create = true) {
        $data = $siteId ? $this->find($siteId) : array();
        if (!$data && $create) {
            $site_code = xsfn_generate_serial('S' . $siteId);
            $data = array(
                'id' => $siteId,
                'site_name' => '网站',
                'site_code' => $site_code,
                'site_app_name' => '网站',
                'site_app_version' => '1.0.0.0',
                'site_sub_path' => '',
                'session_prefix' => 'web' . $site_code,
                'content_list_rows' => 30,
                'site_closed_announcement' => '抱歉,网站正在维护,请您稍后访问~',
                'ui_aside_status' => 'Y',
            );
            $id = $this->isUpdate(false)->save($data);
            $data = $this->find($id);
        }
        return $data;
    }

    public function saveSite() {
        $data = input('post.');
        $tab = empty($data['tab']) ? 'base' : '';
        unset($data['tab']);
        switch ($tab) {
            case 'base':
                if (empty($data['site_name'])) {
                    return xsfn_message(false, '站点名称未填写');
                }
                break;
        }
        if (!empty($data['lang_list'])) {
            $data['lang_list'] = implode(',', $data['lang_list']);
        }
        $result = $this->isUpdate(true)->save($data);
        return xsfn_message($result);
    }

    function changeStatus($id, $status) {
        $result = $this->where(array('id' => $id))->setField(array('status' => $status));
        return xsfn_message($result);
    }

    function changeSort($id, $sort) {
        $result = $this->where(array('id' => $id))->setField(array('sort' => $sort));
        return xsfn_message($result);
    }

    function deleteSite($id) {
        $result = $this->where(array('id' => array('in', $id)))->delete();
        return xsfn_message($result);
    }

}
